//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package project_dao

import (
	"context"

	"atomgit.com/openlinksaas/api-server/dao/db_ns"
	"atomgit.com/openlinksaas/proto-gen-go.git/project_server_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type BasicServerInfoDbItem struct {
	ServerName string   `bson:"serverName"`
	ServerType uint32   `bson:"serverType"`
	AddrList   []string `bson:"addrList"`
}

type ServerInfoDbItem struct {
	ServerId   string                `bson:"_id"`
	ProjectId  string                `bson:"projectId"`
	BasicInfo  BasicServerInfoDbItem `bson:"basicInfo"`
	CreateTime int64                 `bson:"createTime"`
	UpdateTime int64                 `bson:"updateTime"`
}

func (item *ServerInfoDbItem) ToServerInfo() *project_server_api.ServerInfo {
	return &project_server_api.ServerInfo{
		ServerId: item.ServerId,
		BasicInfo: &project_server_api.BasicServerInfo{
			ServerName: item.BasicInfo.ServerName,
			ServerType: project_server_api.SERVER_TYPE(item.BasicInfo.ServerType),
			AddrList:   item.BasicInfo.AddrList,
		},
		CreateTime: item.CreateTime,
		UpdateTime: item.UpdateTime,
	}
}

type _ServerDao struct {
	mc *mongo.Collection
}

func newServerDao(db *mongo.Database) (*_ServerDao, error) {
	mc := db.Collection(db_ns.DB_NS_PROJECT + "server")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"projectId", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_ServerDao{mc: mc}, nil
}

func (db *_ServerDao) Insert(ctx context.Context, dbItem *ServerInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_ServerDao) Exist(ctx context.Context, projectId, serverId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": serverId, "projectId": projectId})
	return count > 0, err
}

func (db *_ServerDao) Get(ctx context.Context, projectId, serverId string) (*ServerInfoDbItem, error) {
	retItem := &ServerInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": serverId, "projectId": projectId}).Decode(retItem)
	return retItem, err
}

func (db *_ServerDao) Remove(ctx context.Context, projectId, serverId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": serverId, "projectId": projectId})
	return err
}

func (db *_ServerDao) List(ctx context.Context, projectId string) ([]*ServerInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"projectId": projectId}, options.Find().SetSort(bson.M{"createTime": 1}))
	if err != nil {
		return nil, err
	}
	retList := []*ServerInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_ServerDao) Update(ctx context.Context, projectId, serverId string, dbItem *BasicServerInfoDbItem, updateTime int64) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": serverId, "projectId": projectId},
		bson.M{"$set": bson.M{
			"basicInfo":  dbItem,
			"updateTime": updateTime,
		}})
	return err
}
